<?php
namespace App\Services\User;
use App\Common\Constant;
use App\Common\ResponseCode;
use App\Exceptions\BusinessException;
use App\Common\Tools;
use App\Models\Base\Group;
use App\Models\Role\Role;
use App\Models\User\User;
use App\Params\Backend\RoleListParam;
use App\Params\Backend\UserListParam;
use App\Params\Backend\UserUpdateParam;
use App\Repository\GroupRepository;
use App\Repository\RoleRepository;
use App\Repository\UserRepository;
use App\Services\BaseService;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
/**
 * 用户相关业务类
 *
 * @author WangBin
 * @date 2021-09-24
 * @package App\Http\Services
 */
class UserService extends BaseService
{
    /**
     * 根据账户密码登录
     *
     * @param string $username
     * @param string $password
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object
     * @throws BusinessException
     */
    public function loginByPwd(string $username, string $password)
    {
        $user = UserRepository::getInstance()->getByUsername($username);
        if(is_null($user)) {
            throw new BusinessException(ResponseCode::USER_NOT_FOUND);
        }
        if(!Hash::check($password, $user->password)) {
            throw new BusinessException(ResponseCode::LOGIN_FAIL);
        }
        return $user;
    }
    /**
     * 保存用户信息
     *
     * @param UserUpdateParam $param)
     * @return mixed
     * @throws BusinessException
     */
    public function update(UserUpdateParam $param)
    {
        // 更新用户
        $user = UserRepository::getInstance()->getById($param->getUserId());
        if(is_null($user)) {
            throw new BusinessException(ResponseCode::USER_NOT_FOUND);
        }
        return UserRepository::getInstance()->update($param->toArray(), $param->getUserId());
    }

    /**
     * 重置密码
     *
     * @param int $userId
     * @param string $password
     * @throws
     * @return mixed
     */
    public function reset(int $userId, string $password)
    {
        $user = UserRepository::getInstance()->getById($userId);
        if(is_null($user)) {
            throw new BusinessException(ResponseCode::USER_NOT_FOUND);
        }
        $newPassword = Hash::make(config('app.secret') . $password);
        return UserRepository::getInstance()->update(['password' => $newPassword], $userId);
    }

    /**
     * 获取用户信息
     *
     * @param int $userId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @throws BusinessException
     */
    public function get(int $userId)
    {
        return UserRepository::getInstance()->getById($userId);
    }

    /**
     * 管理员列表
     *
     * @param UserListParam $param
     * @return array
     */
    public function getList(UserListParam $param)
    {
        $result = $this->toList(UserRepository::getInstance()->getList($param->toArray()), $param->getPage(), $param->getPageSize());
        // 处理角色数据
        collect($result['list'])->each(function ($item){
            $roles = RoleRepository::getInstance()->getByIds($item->role_id);
            $item->role_name = implode(';', $roles->pluck('role_name')->all());
            $item->group_name = GroupRepository::getInstance()->getById($item->group_id)->group_name;
        });
        return $result;
    }

    /**
     * 删除管理员
     *
     * @param int $userId
     * @return mixed
     */
    public function delete(int $userId)
    {
        return UserRepository::getInstance()->delete($userId);
    }

    /**
     * 获取用户初始化数据
     *
     * @return array
     */
    public function getInit()
    {
        return [
            'role_list' => RoleRepository::getInstance()->getList()->get(),
            'group_list' => $this->toSimpleList(GroupRepository::getInstance()->getList(['type' => Constant::GROUP_TYPE_USER])),
        ];
    }
}
